package com.wargame.engine;

import com.wargame.hexmap.HexagonalMap;
import com.wargame.hexmap.Offset;
import com.wargame.util.ChangeType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 算子机动方法——检查并修改算子状态
 *
 * @param thisOpr 算子对象
 * @param action 算子对象
 * @param map 地图对象
 * @return Object 机动方法产生null的返回值
 * */
public Object CalloutMoveAction(Operator thisOpr, Action action, HexagonalMap map) throws ClassNotFoundException {

    int objID = (int) thisOpr.getAttri("ObjID");
    double speed = (double) thisOpr.getAttri("Speed");
    boolean canToMove = (boolean) thisOpr.getAttri("CanToMove");
    double moveToStopRemainTime = (double) thisOpr.getAttri("MoveToStopRemainTime");
    int changeStateRemainTime = (int) thisOpr.getAttri("ChangeStateRemainTime");
    int weaponUnfoldTime = (int) thisOpr.getAttri("WeaponUnfoldTime");
    String moveState = (String) thisOpr.getAttri("MoveState");
    int[] curHex = (int[]) thisOpr.getAttri("CurHex");
    String subType = (String) thisOpr.getAttri("SubType");
    String type = (String) thisOpr.getAttri("Type");
    double basicSpeed = (double) thisOpr.getAttri("BasicSpeed");
    List<String> states = (List<String>) thisOpr.getAttri("States");
    boolean keep = (boolean) thisOpr.getAttri("Keep");

    // List<int[]> movePath = (List<int[]>) action.getAttri("MovePath");
    int actionObjID = (int) action.getAttri("ObjID");
    List<int[]> movePath = (int[]) action.getAttri("MovePath");
    String name = (String) action.getAttri("Name");

    boolean flag_move = true;   // 可以机动的标志位
    int moveMode = -1; // 通行方式
    double curSpeed = 0.0; // 通行速度
    if (actionObjID != objID)
    {
        System.out.println("无效动作：算子不为执行动作的算子");
        flag_move = false;
    }else if (!name.equals("Move"))
    {
        System.out.println("无效动作：算子的动作类型有误，应该是机动类型");
        flag_move = false;
    }else if (movePath.size() <= 0)
    {
        System.out.println("无效动作：算子的动作路径为空");
        flag_move = false;
    }else if (speed > 0) {
        System.out.println("无效动作：未停下来的算子"+objID+"不可下达机动命令");
        flag_move = false;
    } else if (!canToMove && moveToStopRemainTime > 0)  { // 正处于强制停止的判断

        System.out.println("无效动作：强制停止中的算子不可下达机动命令");
        flag_move = false;
    }else if (changeStateRemainTime > 0) {
        System.out.println("无效动作：算子正在切换状态,不能机动");
        flag_move = false;
    } else if (weaponUnfoldTime > 0) {
        System.out.println("无效动作：算子正在展开或者锁定武器,不能机动");
        flag_move = false;
    }
    else if (subType.equals("Infantry") && keep){
        System.out.println("无效动作：人员算子被压制,不能机动");
        flag_move = false;
    }

//         1.行军状态的可机动的判断
    if (moveState.equals("March")) {
        moveMode = 1; // 通行方式为车辆行军方式
        int index = map.getNeighborIndex(new Offset(curHex[0], curHex[1]), new Offset(movePath.get(0)[0], movePath.get(0)[1]));
        if (index != -1){
            //取得道路类型
            int roadType = map.getNeighborRoad(new Offset(curHex[0], curHex[1]), new Offset(movePath.get(0)[0], movePath.get(0)[1]));
            switch (roadType) {
                case 1: curSpeed = 40; break;// 如果是乡村路（黑色线）40千米/小时
                case 2: curSpeed = 60; break;// 如果是一般公路（红色线）60千米/小时
                case 3: curSpeed = 90; break;// 如果是等级公路（黄色线）90千米/小时
                default: curSpeed = 0; break;// 未定义的道路类型
            }
            if (curSpeed == 0) {
                System.out.println("无效动作：算子"+objID+"行军路径有误");
                return null;
            }
        }else{
            System.out.println("无效动作：算子"+objID+"行军路径有误，六角格坐标不连续");
            flag_move = false;
        }

    }
    // 2.非行军状态的可机动的判断
    else if (moveState.equals("Normal") || moveState.equals("Shelt") || moveState.equals("HalfSpeed")) {// 正常机动，掩蔽，半速

        if (type.equals("Vehicle")) moveMode = 0; // 车辆机动
        else if (type.equals("Infantry")) moveMode = 2;// 步兵机动
        else if (type.equals("Airplane")) moveMode = 3;// 空中机动
        else {
            System.out.println("无效动作：未知的算子类型："+type);
            flag_move = false;
        }
        if (moveState.equals("HalfSpeed")) curSpeed = basicSpeed / 2;
        else curSpeed = basicSpeed; 

    }else {
        System.out.println("无效动作：未定义的机动状态"+moveState);
        flag_move = false;
    }
    // 完成算子状态的判断，状态不允许机动则退出方法
    if (!flag_move) return null;

    if (moveMode == -1 || curSpeed == 0) {
        System.out.println("无效动作：通信方式或者速度为空，通行方式："+moveMode+"，速度："+curSpeed);
        return null;
    }
    // 可机动时，判断机动路径
    // 判断当前位置到move_path的第一个六角格是否有道路
    double cost = map.getCost(new Offset(curHex[0], curHex[1]), new Offset(movePath.get(0)[0], movePath.get(0)[1]),moveMode);
    if (cost == 0){
        System.out.println("无效动作：算子行军路线不通");
        return null;
    }
    for (int i = 0; i < movePath.size() - 1; i++) {
        int[] cur_hex = movePath.get(i);// 本格
        int[] next_hex = movePath.get(i + 1);// 下一格
        cost = map.getCost(new Offset(cur_hex[0], cur_hex[1]), new Offset(next_hex[0], next_hex[1]),moveMode);
        if (cost == 0){
            System.out.println("无效动作：算子行军路线不通");
            return null;
        }
    }
//        if (!map.getNeighbors(curHex[0], curHex[1]).contains(new Offset(movePath.get(0)[0], movePath.get(0)[1]))) {
//            System.out.println("无效动作：算子行军路线不通");
//            flag_move = false;
//        } else if (map.getNeighborRoad(new Offset(curHex[0], curHex[1]), new Offset(movePath.get(0)[0], movePath.get(0)[1])) <= 0) {
//            System.out.println("无效动作：算子行军路线不通");
//            flag_move = false;
//        }
//        else {
//            // 判断move_path中每个六角格之间是不是通的
//            for (int i = 0; i < movePath.size() - 1; i++) {
//                int[] cur_hex = movePath.get(i);// 本格
//                Offset curOffset = new Offset(cur_hex[0], cur_hex[1]);
//                int[] next_hex = movePath.get(i + 1);// 下一格
//                Offset nextOffset = new Offset(next_hex[0], next_hex[1]);
//                if (!map.getNeighbors(curOffset).contains(nextOffset)) {
//                    System.out.println("无效动作：算子行军路线不通");
//                    flag_move = false;
//                    break;
//                } else if (map.getNeighborRoad(curOffset, nextOffset) <= 0) {
//                    System.out.println("无效动作：算子行军路线不通");
//                    flag_move = false;
//                    break;
//                }
//            }
//        }

    if (moveState.equals("Shelt"))
        thisOpr.setAttri("MoveState","Normal");
    speed = (int)(curSpeed / (float) cost);// 修改速度，基础速度除通行代价
    thisOpr.setAttri("Speed",speed);
    thisOpr.setAttri("Stop",false);
    thisOpr.setAttri("MovePath",movePath);
    thisOpr.setAttri("MoveToStopRemainTime",0);

    /*
     * 在每一轮的更新中修改cur_hex, cur_pos等状态值，不在本方法中更新
     */
    return null;
}


